package cn.liyunqi.ttv.web.service.impl;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.MD5Util;
import cn.liyunqi.core.util.TimeUtil;
import cn.liyunqi.core.util.UUIDUtil;
import cn.liyunqi.ttv.web.bo.MailLogInfo;
import cn.liyunqi.ttv.web.bo.UserInfo;
import cn.liyunqi.ttv.web.mapper.MailLogInfoMapper;
import cn.liyunqi.ttv.web.mapper.UserInfoMapper;
import cn.liyunqi.ttv.web.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private MailLogInfoMapper mailLogInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void register(String mail, String mailCode, String password, String recommendMail) {
        Long nowTime = TimeUtil.getTime();

        mailCode = mailCode.toUpperCase();

        List<MailLogInfo> mailLogInfoList = mailLogInfoMapper.selectEffectiveCodeByMailAndCode(mail,mailCode,1,nowTime - 300000);
        if(mailLogInfoList.size() > 0){

            UserInfo  temp = userInfoMapper.selectUserInfoByMail(mail);
            if(temp != null && temp.getIsBlack() == 1){
                throw new BizException("当前邮箱已被加入黑名单");
            }else if(temp != null){
                throw new BizException("该邮箱已注册");
            }
            UserInfo  recommend = userInfoMapper.selectUserInfoByMail(recommendMail);

            mailLogInfoMapper.updateEffectiveCodeByMailAndCode(mail,mailCode,1);

            UserInfo userInfo = new UserInfo();
            userInfo.setId(UUIDUtil.getUUID());
            userInfo.setCreateTime(nowTime);
            userInfo.setIsBlack(0);
            userInfo.setIsDelete(0);
            userInfo.setMail(mail);
            userInfo.setNickName("");
            userInfo.setPassword(MD5Util.MD5(password));
            //userInfo.setLimitTime(recommend != null?nowTime + 1000*60*60*24*4:nowTime + 1000*60*60*24);
            userInfo.setLimitTime(1546272000000L);

            userInfo.setRecommendMailId(recommend != null?recommend.getId():"");
            userInfo.setIsRecharge(0);
            userInfo.setToken("");
            userInfoMapper.insert(userInfo);

        }else{
            throw new BizException("无效验证码或者验证码已过期");
        }
    }

    public UserInfo login(String mail, String password) throws BizException {
        UserInfo bo = userInfoMapper.selectUserInfoByMail(mail);
        if(bo == null){
            throw new BizException("用户不存在");
        }
        if(bo.getIsDelete() == 1){
            throw new BizException("用户不存在");
        }
        if(bo.getIsBlack() == 1){
            throw new BizException("用户已被加入黑名单");
        }
        if(!bo.getPassword().equals(MD5Util.MD5(password))){
            throw new BizException("密码错误");
        }
        String token = UUIDUtil.getUUID();
        bo.setToken(token);

        UserInfo temp = new UserInfo();
        temp.setId(bo.getId());
        temp.setToken(token);
        userInfoMapper.updateByPrimaryKeySelective(temp);

        bo.setPassword("");
        return bo;
    }

    @Override
    public void resetPassword(String mail, String mailCode, String password) {
        Long nowTime = TimeUtil.getTime();

        mailCode = mailCode.toUpperCase();

        List<MailLogInfo> mailLogInfoList = mailLogInfoMapper.selectEffectiveCodeByMailAndCode(mail,mailCode,2,nowTime - 300000);
        if(mailLogInfoList.size() > 0){

            UserInfo  temp = userInfoMapper.selectUserInfoByMail(mail);
            if(temp == null || temp.getIsBlack() == 1){
                throw new BizException("该邮箱不存在");
            }

            mailLogInfoMapper.updateEffectiveCodeByMailAndCode(mail,mailCode,1);

            temp.setCreateTime(nowTime);

            temp.setPassword(MD5Util.MD5(password));

            userInfoMapper.updateByPrimaryKeySelective(temp);

        }else{
            throw new BizException("无效验证码或者验证码已过期");
        }
    }

    @Override
    public String checkAuth(String userId, String token) {

        UserInfo userInfo= userInfoMapper.selectByPrimaryKey(userId);
        if(userInfo == null || userInfo.getIsDelete() == 1){
            throw new BizException("该会员不存在请重新登录","0501");
        }
        if(userInfo.getIsBlack() == 1){
            throw new BizException("该会员已被加入黑名单","0501");
        }
        if(!token.equals(userInfo.getToken())){
            throw new BizException("账号登录过期，请重新登录","0501");
        }
        if(userInfo.getLimitTime() < TimeUtil.getTime()){
            throw new BizException("会员过期,请充值","0502");
        }

        String tokenNew = UUIDUtil.getUUID();
        userInfo.setToken(tokenNew);
        userInfoMapper.updateByPrimaryKey(userInfo);

        return tokenNew;
    }


}
